using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using CoreManager.Repository.DbContext;

// 简单的测试程序来验证种子数据
var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("CoreManager.API/appsettings.json")
    .Build();

var services = new ServiceCollection();

// 配置EF Core
var connectionString = configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<CoreManagerDbContext>(options =>
{
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
    options.EnableSensitiveDataLogging();
    options.EnableDetailedErrors();
});

// 添加其他必需的服务
services.AddLogging(builder => builder.AddConsole());
services.AddHttpContextAccessor();
services.AddSingleton<IConfiguration>(configuration);

var serviceProvider = services.BuildServiceProvider();

try
{
    using var scope = serviceProvider.CreateScope();
    var context = scope.ServiceProvider.GetRequiredService<CoreManagerDbContext>();
    
    Console.WriteLine("正在初始化数据库...");
    await context.InitializeDatabaseAsync();
    
    Console.WriteLine("检查用户数据...");
    var users = await context.Users.ToListAsync();
    Console.WriteLine($"用户总数: {users.Count}");
    
    foreach (var user in users)
    {
        Console.WriteLine($"用户: {user.UserName}, 真实姓名: {user.RealName}, 状态: {user.Status}");
    }
    
    var roles = await context.Roles.ToListAsync();
    Console.WriteLine($"角色总数: {roles.Count}");
    
    foreach (var role in roles)
    {
        Console.WriteLine($"角色: {role.RoleName}, 编码: {role.RoleCode}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"错误: {ex.Message}");
    Console.WriteLine($"详细信息: {ex}");
}

Console.WriteLine("测试完成，按任意键退出...");
Console.ReadKey();